package org.jeecg.modules.goods.whLocation.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.jeecg.modules.goods.whLocation.entity.WhLocation;
import org.jeecg.modules.goods.whLocation.mapper.WhLocationMapper;
import org.jeecg.modules.goods.whLocation.service.IWhLocationService;
import org.jeecg.modules.tplus.dto.InvLocationQueryRepDto;
import org.jeecg.modules.tplus.dto.InvLocationQueryReqDto;
import org.jeecg.modules.tplus.service.ITplusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 货位管理
 * @Author: jeecg-boot
 * @Date:   2025-08-12
 * @Version: V1.0
 */
@Service
public class WhLocationServiceImpl extends ServiceImpl<WhLocationMapper, WhLocation> implements IWhLocationService {
    @Autowired
    private ITplusService tplusService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleLocation(){
        List<WhLocation> whLocationList = this.list();
        if(CollectionUtil.isNotEmpty(whLocationList)) {
            baseMapper.deleteBatchIds(whLocationList);
        }
        List<InvLocationQueryRepDto> invLocationQueryRepDtos = tplusService.invLocationQuery(new InvLocationQueryReqDto());
        List<WhLocation> locations = new ArrayList<>();
        for (InvLocationQueryRepDto invLocationQueryRepDto : invLocationQueryRepDtos) {
            WhLocation whLocation = new WhLocation();
            whLocation.setLocationCode(invLocationQueryRepDto.getCode());
            whLocation.setLocationName(invLocationQueryRepDto.getName());
            whLocation.setWarehouseBelongsTo(invLocationQueryRepDto.getWarehouseCode());
            locations.add(whLocation);
        }
        super.saveOrUpdateBatch(locations);
    }
}
